import { getClassifyApi, postMsbdApi, getClassifysApi, deleteMsbdApi, updateMsbdApi } from '../../api/msbd'

export default {
  namespaced: true,
  state: {
    list: [],
    classify: [],
    sort: 'desc',
    orderby: 'id',
    pageSize: 10,
    currentPage: 1,
    total: 0
  },

  mutations: {
    SET_LIST(state, list) {
      state.list = list
    },
    SET_CLASSIFY(state, classify) {
      state.classify = classify
    },
    SET_TOTAL(state, total) {
      state.total = total
    },
    SET_CURR(state, val) {
      state.currentPage = val
    },
    SET_SIZE(state, val) {
      state.pageSize = val
    }
  },

  actions: {
    async getList({ commit, state }, searchValue) {
      let r = await getClassifyApi({ sort: state.sort, orderby: state.orderby, pageSize: state.pageSize, currentPage: state.currentPage-1, searchValue: searchValue.search })
      commit('SET_LIST', r.data)
      commit('SET_TOTAL', r.total[0].total)
      // console.log(r)
    },
    async postMsbd({ commit }, form) {
      await postMsbdApi(form)
    },
    async getClassify({ commit }) {
      let r = await getClassifysApi()
      commit('SET_CLASSIFY', r.data)
    },
    async deleteMsbd({ commit }, id) {
      console.log(id)
      await deleteMsbdApi(id)
    },
    async updateMsbd({ commit }, r) {
      await updateMsbdApi(r.form)
    }
  },

  getters: {}
}
